######################################################################
# Jun Yang (junyang@cs.duke.edu)
#
# Usage:
#	make		Compile the whole thing and produce ${base}.pdf.
#			Figure sources files in ${figs}/ directory will be
#			automatically converted to file formats appropriate
#                       for inclusion.
#	make clean	Remove temp and backup files automatically
#			created.  Will not remove ${base}.pdf.
#	make archive	Create a .tgz archive in the parent directory
#			for this entire directory.  Will follow symbolic
#			links and archive those contents too.
#	make freeze	Recursively replace symbolic links with actual
#			contents pointed to by them.
#	make nsf	Extract components from ${base}.pdf for FastLane
#			upload.

######################################################################
# Name of the main latex file (without .tex suffix); make will produce
# a PDF file with this name and .pdf suffix:
base = paper
# Directory containing all figures:
figs = figs

# Source .eps files; make will automatically convert them into .pdf files
# for embedding these fitures into latex:
eps_src = $(shell find ${figs} -type f -name '*.eps')
eps_pdf = $(addsuffix .pdf,$(basename ${eps_src}))

# Source .fig (xfig) files; make will automatically produce .pdf and .tex
# files for embedding these figures into latex:
xfig_src = $(shell find ${figs} -type f -name '*.fig')
xfig_pdf = $(addsuffix .pdf,$(basename ${xfig_src}))
xfig_pdftex = $(addsuffix .pdf_t,$(basename ${xfig_src}))

# Source .tikz.py files; make will automatically run them to produce .tikz
# files for embedding these figures into latex:
tikz_src = $(shell find ${figs} -type f -name '*.tikz.py')
tikz_tikz = $(basename ${tikz_src})

######################################################################
# Commands:

PDFLATEX = pdflatex
# The following option to bibtex in effect forces inlining of the
# content of a crossref (rather than creating a separate bibitem for
# the crossref and referencing it):
BIBTEX = bibtex -min-crossrefs=100
FIG = xfig -spec
FIG2PDF = fig2dev -L pdftex -p portrait
FIG2PDFTEX = fig2dev -L pdftex_t -p
PYTHON = python
EPS2PDF = ps2pdf -dEPSCrop
#EPS2PDF = ps2pdf -dEPSFitPage

######################################################################
# Main rules:

.PHONY : ${base}.pdf clean archive freeze nsf

${base}.pdf : ${eps_pdf} ${xfig_pdf} ${xfig_pdftex} ${tikz_tikz}
	${PDFLATEX} ${base}
	${BIBTEX} ${base}
	${PDFLATEX} ${base}
	${PDFLATEX} ${base}

${eps_pdf} : %.pdf : %.eps
	${EPS2PDF} $< $@

${xfig_pdf} : %.pdf : %.fig
	${FIG2PDF} $< $@

${xfig_pdftex} : %.pdf_t : %.pdf

${xfig_pdftex} : %.pdf_t : %.fig
	${FIG2PDFTEX} $(addsuffix .pdf,$(basename $<)) $< $@

${tikz_tikz} : %.tikz : %.tikz.py
	${PYTHON} $< > $@

clean :
	rm -f *.bak *~ $(addsuffix .bak,${xfig_src})
	rm -rf auto/
	rm -f ${eps_pdf}
	rm -f ${xfig_pdf} ${xfig_pdftex}
	rm -f ${tikz_tikz}
	rm -f ${base}.log ${base}.aux ${base}.blg ${base}.bbl

DIR_NAME = $(notdir ${CURDIR})
ARCHIVE_TS_NAME = ${DIR_NAME}-$(shell stat -c %y ${base}.pdf | sed 's/:[0-9]*\..*$$//; s/ /-/; s/:/-/')

archive :
	ln -sf `pwd` /tmp/${ARCHIVE_TS_NAME}
	tar -chvzf ../${ARCHIVE_TS_NAME}.tgz -C /tmp ${ARCHIVE_TS_NAME}
	rm -f /tmp/${ARCHIVE_TS_NAME}

nsf : ${base}.pdf
	gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=1 -dLastPage=1 -sOutputFile=summary.pdf ${base}.pdf
	gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=2 -dLastPage=16 -sOutputFile=description.pdf ${base}.pdf
	gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=17 -dLastPage=17 -sOutputFile=data.pdf ${base}.pdf
	gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=18 -sOutputFile=references.pdf ${base}.pdf

-include Makefile.more

